


import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;


public class jdbcDemo {
    public static void main(String[] args) throws SQLException{
      // 1.数据源
        DataSource dataSource = new MysqlDataSource();
        ((MysqlDataSource) dataSource).setUrl("jdbc:mysql://127.0.0.1:3306/java111?characterEncoding=utf8&useSSL=false");
        ((MysqlDataSource) dataSource).setUser("root");
        ((MysqlDataSource) dataSource).setPassword("123456");
        //2.连接
        Connection connection =  dataSource.getConnection();
        //3.构造sql语句
        String sql = "insert into student values(1,'张三')";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        //4。执行sql
        int n = preparedStatement.executeUpdate();
        System.out.println("n = "+ n);
        //5，；回收资源
        preparedStatement.close();
        connection.close();
    }
}
